package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCacheAnnotation;
import com.baizhi.cmfz.dao.BannerDao;
import com.baizhi.cmfz.entity.Banner;
import com.baizhi.cmfz.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerDao bannerDao;

    @Override
    @DeleteCacheAnnotation
    public int addBanner(Banner banner) {
        return bannerDao.insertBanner(banner);
    }

    @Override
    @DeleteCacheAnnotation
    public int removeBanner(int[] ids) {
        return bannerDao.deleteBanner(ids);
    }

    @Override
    @DeleteCacheAnnotation
    public int updateBanner(Banner banner) {
        return bannerDao.updateBanner(banner);
    }

    @Override
    @Transactional(readOnly = true)
    @AddCacheAnnotation
    public Banner selectOneBanner(int bannerId) {
        return bannerDao.selectBannerById(bannerId);
    }

    @Override
    @Transactional(readOnly = true)
    @AddCacheAnnotation
    public Map selectAllBanner(int page, int limit) {
        page = (page - 1) * limit;
        Map<String, Object> map = new HashMap<>();
        map.put("data", bannerDao.selectAllByPage(page, limit));
        map.put("count", bannerDao.countBanner());
        map.put("code", 0);
        return map;
    }

    @Override
    public List<Banner> findBannerList(Integer count) {
        if (count == 0) {
            count = null;
        }
        return bannerDao.findBannerList(count);
    }
}
